package com.linked.goods.goodsbase.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.goods.config.RedisKeysConfig;
import com.linked.goods.goodsbase.bean.dto.GoodsVersionDTO;
import com.linked.goods.goodsbase.bean.param.GoodsQueryParam;
import com.linked.goods.goodsbase.bean.view.GoodsQueryView;
import com.linked.goods.goodsbase.service.IGoodsQueryService;
import com.linked.goods.goodsbase.service.IGoodsVersionService;
import com.linked.goods.util.HandleRedisUtils;
import com.linked.universal.bean.goods.goods.LinkedGoods;
import com.linked.universal.common.LinkedPrompt;
import com.linked.universal.common.LinkedResult;
import com.linked.universal.common.Result;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :dbq
 * @date : 2023/1/28 9:38
 * @description : desc
 */
@RestController
@Slf4j
@RequestMapping("goodsquery")
public class GoodsQueryController {

    private final IGoodsQueryService goodsQueryService;

    private final IGoodsVersionService goodsVersionService;

    private final HandleRedisUtils<GoodsVersionDTO> handleRedisUtils;

    private final ObjectMapper mapper;

    public GoodsQueryController(HandleRedisUtils handleRedisUtils, IGoodsVersionService goodsVersionService, IGoodsQueryService goodsQueryService, ObjectMapper mapper) {
        this.goodsQueryService = goodsQueryService;
        this.goodsVersionService = goodsVersionService;
        this.handleRedisUtils = handleRedisUtils;
        this.mapper = mapper;
    }

    //
    @PostMapping("queryGoodsPage")
    Result queryGoodsPage(@RequestBody GoodsQueryParam param) {

        IPage<GoodsQueryView> goodsPage = null;
        try {
            goodsPage = goodsQueryService.queryGoodsPage(param);
        } catch (Exception e) {
            log.error("查询商品页面接口 异常", e);
            return Result.error("未知异常，请联系管理员！");
        }
        return goodsPage != null ? Result.success(goodsPage) : Result.ok(false, "未查询到数据！");
    }

    @PostMapping("queryGoodsVersions")
    Result queryGoodsVersions(String goodsId) {
        if (log.isInfoEnabled()) {
            log.info("查询商品版本接口 入参：" + goodsId);
        }
        List<GoodsVersionDTO> versionList = null;
        try {
            versionList = handleRedisUtils.getRedisHashList(RedisKeysConfig.KEY_GOODSVERSIONS, goodsId, goodsQueryService::queryGoodsVersionList);
        } catch (Exception e) {
            log.error("查询商品版本接口 异常！", e);
            return Result.error("未知异常，请联系管理员！");
        }
        return !CollectionUtils.isEmpty(versionList) ? Result.success(versionList) : Result.ok(false, "未查询到版本信息");
    }

    @PostMapping("queryLinkedGoods")
    LinkedGoods queryLinkedGoods(@NonNull String goodVersionId) {
        if (log.isInfoEnabled()) {
            log.info("查询linked商品借口 goodVersionId：" + goodVersionId);
        }
        LinkedGoods resGoods = null;
        try {
            resGoods = goodsQueryService.queryLinkedGoods(goodVersionId);
        } catch (Exception e) {
            log.error("查询linked商品借口 异常", e);
            return null;
        }
        return resGoods;
    }

}
